﻿#include "PlatformProperty.h"

#define HVAC_FAN_STATUS_MANAUAL 0

MAPPING_VALUE(sdb::AvailabilitySts, PropertyStatus, PropertyStatus::NotAvailable)
    MAPPING_ITEM(sdb::AvailabilitySts::Active, PropertyStatus::Active)
    MAPPING_ITEM(sdb::AvailabilitySts::NotActive, PropertyStatus::NotActive)
    MAPPING_ITEM(sdb::AvailabilitySts::Error, PropertyStatus::Error)
    MAPPING_ITEM(sdb::AvailabilitySts::NotAvailable, PropertyStatus::NotAvailable)
MAPPING_END

MAPPING_VALUE(FanSpeedLevel, sdb::HmiHvacFanLvl, sdb::HmiHvacFanLvl::Off)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_OFF, sdb::HmiHvacFanLvl::Off)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_1, sdb::HmiHvacFanLvl::LvlMan1)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_2, sdb::HmiHvacFanLvl::LvlMan2)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_3, sdb::HmiHvacFanLvl::LvlMan3)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_4, sdb::HmiHvacFanLvl::LvlMan4)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_5, sdb::HmiHvacFanLvl::LvlMan5)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_6, sdb::HmiHvacFanLvl::LvlMan6)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_7, sdb::HmiHvacFanLvl::LvlMan7)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_8, sdb::HmiHvacFanLvl::LvlMan8)
    MAPPING_ITEM(FanSpeedLevel::FAN_SPEED_LEVEL_9, sdb::HmiHvacFanLvl::LvlMan9)
MAPPING_END

bool fanLevelAvailabilityCheck( int level) {
    if( (level < (int)FanSpeedLevel::FAN_SPEED_OFF) || ( level > (int)FanSpeedLevel::FAN_SPEED_LEVEL_9)){
        return false;
    }
    return true;
}

bool isManualFanLevel(int level) {
    return (toInt(sdb::HmiHvacFanLvl::Off) <= level && toInt(sdb::HmiHvacFanLvl::LvlMan9) >= level);
}

SYSTEM_READ_WRITE_PROPERTY(HVAC_FAN_SPEED, int32_t) {
    builder
        .configArray(std::vector<int32_t>{static_cast<int>(FanSpeedLevel::FAN_SPEED_OFF),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_1),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_2),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_3),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_4),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_5),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_6),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_7),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_8),
                                          static_cast<int>(FanSpeedLevel::FAN_SPEED_LEVEL_9)})
        .area(VehicleAreaSeat::ROW_1_LEFT | VehicleAreaSeat::ROW_1_RIGHT)
        .initValue([]() { return static_cast<int32_t>(FanSpeedLevel::FAN_SPEED_LEVEL_5); })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& lastStatus) {
                PAIntFuncType fanValue = SIGNAL.getPA_CL_FanLevel();
                sdb::AvailabilitySts availabilitySts = sdb::AvailabilitySts(fanValue.availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_FAN_SPEED row_1 status changed to: %d  availabilitySts is: %d isManual:%d", status,
                    availabilitySts, fanValue.status);
                if(fanValue.status == HVAC_FAN_STATUS_MANAUAL)
                {
                    return status;
                }

                return lastStatus;
            },
            PASignal::PA_CL_FanLevel)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t& last) {
                PAIntFuncType fanValue = SIGNAL.getPA_CL_FanLevel();
                int data = SIGNAL.getPA_CL_FanLevel().data;
                if(fanValue.status == HVAC_FAN_STATUS_MANAUAL)
                {
                    VHALOG.d("HVAC_FAN_SPEED row_1 value changed to: %d ", data);
                    return data;
                }

                return last;
            },
            PASignal::PA_CL_FanLevel)
        .onSetProperty([](const int32_t& value) {
            FanSpeedLevel fanSpeedLevel = (FanSpeedLevel)value;
            if( fanLevelAvailabilityCheck(value)) {
                sdb::HmiHvacFanLvl level = mappingValue(fanSpeedLevel, sdb::HmiHvacFanLvl::Off);
                VHALOG.d("halOnSetProperty HVAC_FAN_SPEED row_1: %d level is :%d", value, level);
                SIGNAL.setCB_CL_FanLevel(level);
            } else {
                VHALOG.d("halOnSetProperty HVAC_FAN_SPEED row_1 set err and value is : %d", value);
            }
        });
}
